package com.nexus.all.controller;

import com.nexus.all.dto.NoteDTO;
import com.nexus.all.dto.NoteLikeDTO;
import com.nexus.all.dto.NoteQueryDTO;
import com.nexus.all.es.model.EsNote;
import com.nexus.all.es.service.EsNoteService;
import com.nexus.all.service.NoteService;
import com.nexus.all.vo.NoteVO;
import com.nexus.bean.annotation.PassLogin;
import com.nexus.bean.annotation.valid.AddGroup;
import com.nexus.bean.model.UserInfoVO;
import com.nexus.bean.query.QueryResultVO;
import com.nexus.bean.result.Result;
import com.nexus.web.controller.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @ClassName: NoteController
 * @Author: ZZYR
 * @Date: 2024/2/26  9:54
 * @Version 1.0.0
 **/
@Slf4j
@RequestMapping("/note")
@RestController
@Api(tags = "笔记接口管理")
@AllArgsConstructor
public class NoteController extends CommonController {
    private final NoteService noteService;
    private final EsNoteService esNoteService;


    @ApiOperation(value = "分页查询笔记", notes = "分页查询笔记")
    @PostMapping("/queryNotes")
    @PassLogin
    public Result<QueryResultVO<NoteVO>> queryNotes(@RequestBody NoteQueryDTO dto) {
        UserInfoVO user = getUser();
        if (Objects.nonNull(user)) {
            //登录
            dto.setUserId(user.getId());
        }

        return Result.newSuccess(noteService.queryNotes(dto));
    }

    @ApiOperation(value = "搜索", notes = "搜索")
    @PostMapping("/search")
    @PassLogin
    public Result<Page<EsNote>> search(@RequestBody NoteQueryDTO dto) {
        dto.setPageNo(dto.getPageNo() - 1);
        return Result.newSuccess(esNoteService.search(dto));
    }

    @ApiOperation(value = "发布笔记", notes = "发布笔记")
    @PostMapping("/addNote")
    public Result<Long> addNote(@RequestBody @Validated(AddGroup.class) NoteDTO noteDto) {
        noteDto.setUserId(getUser().getId());
        return Result.newSuccess(noteService.addNote(noteDto));
    }

    @ApiOperation(value = "笔记详情", notes = "笔记详情")
    @GetMapping("/noteDetail/{noteId}")
    @PassLogin
    public Result noteDetail(@ApiParam("笔记id") @PathVariable Long noteId) {
        return Result.newSuccess(noteService.getNoteDetail(noteId, getUser()));
    }

    @ApiOperation(value = "笔记点赞", notes = "笔记点赞")
    @PostMapping("/addLike")
    public Result addLike(@RequestBody @Validated(AddGroup.class) NoteLikeDTO dto) {
        dto.setUserId(getUser().getId());
        noteService.addLike(dto);
        return Result.newSuccess();
    }
}
